package org.timeismoney.payment.web.action.teacher;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.model.Item;
import org.timeismoney.payment.service.ItemService;
import org.timeismoney.payment.web.WebUtils;
import org.timeismoney.payment.web.action.BaseAction;
import org.timeismoney.payment.web.form.ItemForm;

public class AdminItemAction extends BaseAction {
	private static Logger		log	= Logger.getLogger(AdminItemAction.class);
	private ItemService	service;

	public void setService(ItemService service) {
		this.service = service;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//rows=10&page=1
		//sidx	schoolYearNo
		//sord	desc
		String rows = request.getParameter("rows");
		String page = request.getParameter("page");
		String sortCol = request.getParameter("sidx");
		String sortD = request.getParameter("sord");
		int rowsNo = 10;
		try {
			rowsNo = Integer.valueOf(rows);
		} catch (RuntimeException e) {}
		int pageNo = 1;
		try {
			pageNo = Integer.valueOf(page);
		} catch (RuntimeException e) {}
		PageBean<Item> pageBean = new PageBean<Item>(pageNo,rowsNo,sortCol,sortD);
		service.findByPageBean(pageBean);
		renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		return null;
	}
	
	public ActionForward get(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			List<Item> items = service.getAll();
			renderJson(response, WebUtils.generateJSONByCollection(items));
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_ADD+" "+e.getMessage()));
		}
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			ItemForm itemForm = (ItemForm) form;
			Item item = new Item();
			buildModel(itemForm, item);
			service.save(item);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_ADD+" "+e.getMessage()));
		}
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			ItemForm itemForm = (ItemForm) form;
			Item item = new Item();
			buildModel(itemForm, item);
			service.update(item);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_EDIT+" "+e.getMessage()));
		}
		return null;
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			ItemForm itemForm = (ItemForm) form;
			Item item = service.get(itemForm.getItemNo());
			service.delete(item);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(response, WebUtils.generateErrorJSON(TEXT_ERROR_MSG_DEL+" "+e.getMessage()));
		}
		return null;
	}


}
